/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */
#pragma once

#include <Config.h>
#include <xml/Object.h>
#include <lib3d/hard/IndexBuffer.h>
#include <lib3d/hard/VertexBuffer.h>
#include <lib3d/hard/Manager.h>
#include <HardwareDependentObject.h>
#include "PrimitiveObject.h"
#include "Node.h"
#include <lib3d/particles/ParticleSystem.h>
#include <animate/Soul.h>
#include <CompilerFeatures.h>

namespace liba {
namespace lib3d {
namespace node {
			
class Emitter : public Node, public animate::Soul, public HardwareDependentObject<hard::Manager, hard::VertexBuffer>
{
	NODE_DEFINITION(Emitter)
private:
	std::string                  _particleSystemPath;
	particles::ParticleSystem  * _particleSystem;
	particles::Particle        * _particles;
	size_t                       _particlesCount;
	size_t                       _aliveParticles;
	float                        _lastEmitTime;
	float                        _lifeTime;
	bool                         _paused;
	bool                         _needRelease;
	bool				         callBack;
protected:
	virtual void on_add_hardware(hard::Manager::hardware_type * ha) LIBA_OVERRIDE;
	virtual bool on_node(xml::Provider * provider, const std::string & name) LIBA_OVERRIDE;
	virtual bool on_attribute(xml::Provider * provider, const std::string & name, const std::string & value) LIBA_OVERRIDE;
	virtual void on_save_attributes(xml::SaverBase * saver) const LIBA_OVERRIDE;
	virtual void on_save_nodes(xml::SaverBase * saver) const LIBA_OVERRIDE;
	void update_buffer(hard::RenderList * render_list);
public:
	static const Atom node_name;
public:
	Emitter(const Atom & name, Node * parent);
	Emitter(const Emitter & other);
	virtual ~Emitter();
	virtual Emitter * Clone();
	void set_particle_system(particles::ParticleSystem * particle_system);
	particles::ParticleSystem * get_particle_system()
	{
		return this->_particleSystem;
	}
	
	virtual void LifeCycle(time_float delta_time) LIBA_OVERRIDE;
	virtual void RenderToList(hard::Hardware * hardware, hard::RenderList * render_list) LIBA_OVERRIDE;
	
	void pause();
	void resume();
	bool is_paused();
	
	void release();

	void activateParticleSystem(bool _active);
	float getLifeTime() 
	{ 
		return this->_lifeTime; 
	}
	void setLifeTime(float time) 
	{ 
		this->_lifeTime = time; 
	}

	bool CallBack()
	{
		return callBack;
	}
};

}
}
}
